package com.suyeer.basic.util;

import com.suyeer.basic.bean.BaseHttpResContent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author jun 2018/10/19
 */
public class SessionUtil {
    public static void set(BaseHttpResContent hrc, String key, Object val) throws Exception {
        set(hrc.getRequest(), key, val);
    }

    public static void set(HttpServletRequest request, String key, Object val) throws Exception {
        HttpSession httpSession = request.getSession();
        httpSession.setAttribute(key, val);
    }

    public static void remove(BaseHttpResContent hrc, String key) throws Exception {
        remove(hrc.getRequest(), key);
    }

    public static void remove(HttpServletRequest request, String key) throws Exception {
        HttpSession httpSession = request.getSession();
        httpSession.removeAttribute(key);
    }

    public static <T> T get(BaseHttpResContent hrc, String key) throws Exception {
        return get(hrc.getRequest(), key);
    }

    public static <T> T get(HttpServletRequest request, String key) throws Exception {
        HttpSession httpSession = request.getSession();
        return (T) httpSession.getAttribute(key);
    }
}
